<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html class="win-frame">
<head>
<title>代码表管理-新建代码表</title>
<jsp:include page="/common/head.jsp"></jsp:include>
</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>代码表管理</span>
            <span class="split"></span>
            <span>新建代码表</span>
        </div>
        <a id="goBack" class="btn-return distance-left" href="javascript:;"><i class="icon icon-arrow-left"></i>返回上级</a>
    </div>
    <div class="page-title-border"></div>
</div>

<form id="addForm" method="post">
  <div class="form-wrapper">
		<div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>名称</p>
	        <input type="text" id="name" name="name" class="summer-input summer-validatebox" data-validatebox="validType:['required','maxLength[50]']"/>
	     </div>
	     
		<div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>代码</p>
	        <input type="text" id="code" name="code" class="summer-input" />
	     </div>
		<div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>类型</p>
	        <input type="text" id="type" name="type" class="summer-input summer-validatebox" data-validatebox="validType:['required']">
	     </div>
  </div>
  
   <div class="form-btn-wrapper" id="funcbar">
        <a class="btn btn-normal" id="closeBtn">关闭</a>
    </div>
</form>

<script>

	var CODE_CONST_URL = {
		ADD : basePath + "/sys/code/add",
		CHECK_EXIST : basePath + "/sys/code/checkCodeExist",
		INIT_MAIN: basePath + "/sys/code/init"
	};
	
	var permission = CommonUtil.getPermission('code');
	
	$(function() {
		//初始化funcbar
   	    $('#funcbar').prepend($.funcbar({
            type: 'button',
            data: permission,
            permit: ['code:add']
        }));
		
		//加载下拉列表
		initCombobox();
		
		//表单提交事件
		$('#funcbar').on('click', 'a[data-permit="code:add"]', save);

		//关闭按钮事件
		$('#closeBtn, #goBack').on('click', backMain);

		//校验代码code是否可用   
		validateExist('#code', 'code', CODE_CONST_URL.CHECK_EXIST, ['required', 'maxLength[50]', 'regexp' ], "代码可用");
	});
	
	//代码表类型
	function initCombobox() {
		var data = [ {
			"code" : 0,
			"name" : "非级联"
		}, {
			"code" : 1,
			"name" : "级联"
		}

		];
		$('#type').combobox({
			valueField : 'code',
			textField : 'name',
			data : data,
			async : false,
			defaultValue : '0'
		});
	}
	//保存
	function save() {
		$("#addForm").form({
			buttons : ['#saveBtn'],
			ajax : {
				url : CODE_CONST_URL.ADD
			},
			onSubmitSuccess : function(data) {
				 $.stip({
	  	                type:'ok',//显示类型 有info、 ok、 danger
	  	                text: '保存成功！',
	  	                onBeforeHide: function () {
	                   	    backMain();
	  	               }
	             });
			}
		}).form("submit");
	}

	//关闭
	function backMain() {
		window.location.href = CODE_CONST_URL.INIT_MAIN;
	}

	//校验代码code是否可用
	function validateExist(id, name, url, validType, message) {
		$(id).validatebox({
			rules : {
				regexp : {
					validator : function(value, param) {
						var result = false, params = {}, key = name;
						params[key] = value;

						$.ajax({
							type : 'post',
							url : url,
							data : params,
							dataType : 'json',
							async : false,
							success : function(data) {
								if (data.result == 'exist') {
									result = false;
								} else {
									result = true;
								}
							}
						});
						return result;
					},
					message : message,
				}
			},
			validType : validType
		});
	}
</script>
</body>
</html>